<template>
  <div class="goodslist-container">
    <div class="goods-item" v-for="item in goodslist" :key="item.id" @click="goDetail(item.id)">
      <img :src=" item.img_url" alt="商品图片">
      <h3 class="goods-title">{{ item.title }}</h3>
      <div class="info">
        <p class="price">
          <span class="now">¥{{ item.sell_price }}</span>
          <span class="old">¥{{ item.market_price }}</span>
        </p>
        <p class="info-content">
          <span>热卖中</span>
          <span>剩{{ item.stock_quantity }}件</span>
        </p>
      </div>
    </div>
    <!-- 加载更多按钮区域 -->
    <mt-button type="danger" size="large" @click="getmore()">加载更多</mt-button>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      pageindex: 1, // 用于存储商品页数
      goodslist: [], // 存放获取到的商品数据
      max_page: 1 //  存放商品的最大页数,默认是1
    };
  },
  created() {
    this.getgoodslist();
  },
  methods: {
    getgoodslist() {
      this.$http
        .get("api/getgoodslist/?pageindex=" + this.pageindex)
        .then(result => {
          if (result.data.status === 0) {
            this.max_page = result.data.max_page;
            this.goodslist = this.goodslist.concat(result.data.message);
          }
        });
    },
    getmore() {
      this.pageindex++;
      if (this.pageindex >= this.max_page) {
        return Toast("已经没有更多商品了");
      }
      this.getgoodslist();
    },
    goDetail(id) {
        console.log(this)
      this.$router.push({ name: "goodsinfo", params: { id } });
    }
  }
};
</script>
<style lang="scss" scoped>
.goodslist-container {
  display: flex;
  // 让flex元素换行
  flex-wrap: wrap;
  // 让商品和商品之间的间距以及商品和边框的间距差不多大小
  padding: 7px;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin-bottom: 7px;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 308px;
    img {
      width: 100%;
    }
    .goods-title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          // 删除线
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .info-content {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
      }
    }
  }
}
</style>
